package com.eling.elcms.travel.webapp.controller;

import com.eling.elcms.core.AppContext;
import com.eling.elcms.core.dao.hibernate.search.SearchCondition;
import com.eling.elcms.core.util.PropertyUtils;
import com.eling.elcms.core.webapp.controller.BaseFormController;
import com.eling.elcms.core.webapp.controller.Msg;
import com.eling.elcms.core.webapp.spring.ModelFilling;
import com.eling.elcms.travel.model.ApprovalDefine;
import com.eling.elcms.travel.model.ApprovalDefineItem;
import com.eling.elcms.travel.model.view.ApprovalDefineCond;
import com.eling.elcms.travel.service.IApprovalDefineItemService;
import com.eling.elcms.travel.service.IApprovalDefineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

@Controller
public class ApprovalDefineController extends BaseFormController{
	
	@Autowired 
	private IApprovalDefineService approvalDefineService;
	@Autowired
	private IApprovalDefineItemService approvalDefineItemService;
	
	
	@RequestMapping("/api/approvaldefine/saveapprovaldefine")
	@ResponseBody
	public ApprovalDefine saveApprovalDefine(@ModelAttribute ApprovalDefineCond approvalDefine) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
		ApprovalDefine app = new ApprovalDefine();
		PropertyUtils.copyProperties(app, approvalDefine);
		ArrayList<ApprovalDefineItem> list  = new  ArrayList<ApprovalDefineItem>(approvalDefine.getItems());
		for (ApprovalDefineItem approvalDefineItem : list) {
			approvalDefineItem.setApprovalDefine(app);
		}
		approvalDefineItemService.save(list);
		app.setApprovalDefineItems(new HashSet<>(approvalDefine.getItems()));
		
		app.setSetupTime(new Date());
		app.setUser(AppContext.curUser());

		return approvalDefineService.save(app);
	}
	
	@RequestMapping("/api/approvaldefine/save")
	@ResponseBody
	public ApprovalDefine save(@ModelAttribute@ModelFilling ApprovalDefine approvalDefine) {
	  return approvalDefineService.save(approvalDefine);
	}
	
	@RequestMapping("/api/approvaldefine/blockup")
	@ResponseBody
	public ApprovalDefine blockup(@ModelAttribute@ModelFilling ApprovalDefine approvalDefine) {
	  return approvalDefineService.blockup(approvalDefine);
	}
	
	@RequestMapping("/api/approvaldefine/{pkApprovalDefine}/delete")
	@ResponseBody
	public Msg delete(@PathVariable Long pkApprovalDefine) {
		approvalDefineService.remove(pkApprovalDefine);
	   return new Msg("删除成功");
	}
	
	    
	@RequestMapping("/api/approvaldefine/query")
	@ResponseBody
	public List<ApprovalDefine> query(@ModelAttribute ApprovalDefine cond){
		return approvalDefineService.query(cond);
	  }
	
	@RequestMapping("/api/approvaldefine/search*")
	@ResponseBody
	public List<ApprovalDefine> search(@ModelAttribute SearchCondition sc){
		return approvalDefineService.search(sc);
	  }
	
	

}
